package dk.aiszone.lang.messer.autosource;

import dk.aiszone.lang.autosource.generatorobjects.interfaces.SimpleBlockObject;
import dk.aiszone.lang.messer.autosource.interfaces.AbstractModifiableObject;
import dk.aiszone.lang.messer.autosource.interfaces.ComplexStatement;

/**
 * Representation of the while-statement.
 * 
 * @author Ais
 */
public class WhileStatement extends AbstractModifiableObject implements ComplexStatement, SimpleBlockObject {

    private final BlockStatement body;
    private final SimpleBlockObject condition;

    /**
     * Constructor for the <code>WhileStatement</code>.
     * 
     * @param condition
     *            The condition being tested
     * @param body
     *            The body of the for-statement.
     * @param row
     *            The line of the <code>ModifialbleStatement</code>.
     * @param column
     *            The column of the <code>ModifiableStatement</code>.
     */
    public WhileStatement(SimpleBlockObject condition, BlockStatement body, int row, int column) {
        super(row, column);
        this.condition = condition;
        this.body = body;
    }

    @Override
    public Object clone() {
        return new WhileStatement((SimpleBlockObject)condition.clone(), (BlockStatement)body.clone(), getRow(), getColumn());
    }

    @Override
    public SimpleBlockObject getChild(int index) {
        if (index == 0 && body != null) {
            return body;
        }
        throw new IndexOutOfBoundsException(index + " out of bounds");
    }

    @Override
    public int getChildCount() {
        return (body != null ? 1 : 0);
    }

    @Override
    public int getNumberOfSupportedModifications() {
        return 0;
    }

    @Override
    public void modify(int index) {

    }

    @Override
    public void revert() {

    }

    @Override
    public String toJava() {
        StringBuilder sb = new StringBuilder();
        sb.append("while(").append(condition.toJava()).append(") {\n");
        sb.append(body.toJava());
        sb.append("}");

        return sb.toString();
    }
}